package com.zx.networklib;

/**
 * Created by Administrator on 2018/3/14.
 */

import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Cache;
import com.android.volley.NetworkResponse;
import com.android.volley.NoConnectionError;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * 1.解决没有网络，读取缓存文件 2.volley乱码
 *
 * @author V.Wenju.Tian
 */
public class MStringRequest extends StringRequest {

    private Map<String, String> params;

    public MStringRequest(int method, String url, Map<String, String> params,
                          Listener<String> listener, ErrorListener errorListener) {
        super(method, url, listener, errorListener);
        this.params = params;
        // TODO Auto-generated constructor stub
    }

    public MStringRequest(int method, String url, Listener<String> listener, ErrorListener errorListener, Response.LoadingListener loadingListener) {
        super(method, url, listener, errorListener, loadingListener);
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return params;
    }

    // volley缓存是根据URl进行文件缓存的，有时候url一样但参数不一样，所以要进行变化
    @Override
    public String getCacheKey() {
        // TODO Auto-generated method stub
        if (params != null) {

            Log.e("nnnnmmmm", params.toString().hashCode() + "");
            return getUrl() + params.toString().hashCode();
        } else {
            return getUrl();
        }
    }

    @Override
    public void deliverError(VolleyError error) {
        // TODO Auto-generated method stub
        if (error instanceof NoConnectionError) {
            Cache.Entry entry = this.getCacheEntry();
            if (entry != null) {
                // 解析entry 并封装成response
                Response<String> response = parseNetworkResponse(new NetworkResponse(
                        entry.data, entry.responseHeaders));
                // 发送结果到主线程 也就是成功的回调
                deliverResponse(response.result);
                return;
            }
        }
        super.deliverError(error);
    }

    /**
     * 解决volley乱码的问题
     */
    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        // TODO Auto-generated method stub
        try {
            String jsonString = new String(response.data, "UTF-8");
            return Response.success(jsonString,
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (Exception je) {
            return Response.error(new ParseError(je));
        }

    }
}